import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

import butines.core.Game;
import butines.core.GameContainer;
import butines.core.GameContext;
import butines.core.Image;
import butines.core.ImageSheet;
import butines.core.graphics.G2D;

public class SpriteSplit implements Game {

	private GameContext ctx;
	
	private ImageSheet spriteSheet;
	
	public void init(GameContext ctx) {
		this.ctx = ctx;
		
		Image image = new Image("summer.png");
		spriteSheet = new ImageSheet(image, 32, 32);
		System.out.println(spriteSheet.getNumTile());
	}

	public void destroy() {

	}

	public void update(float tpf) {
		if (ctx.getInput().isKeyPressed(KeyEvent.VK_ESCAPE)) {
			ctx.exit();
		}
		
		if (ctx.getInput().isKeyPressed(KeyEvent.VK_UP)) {
			y--;
		}
		if (ctx.getInput().isKeyPressed(KeyEvent.VK_DOWN)) {
			y++;
		}
		if (ctx.getInput().isKeyPressed(KeyEvent.VK_LEFT)) {
			x--;
		}
		if (ctx.getInput().isKeyPressed(KeyEvent.VK_RIGHT)) {
			x++;
		}
		//x = Math.min(spriteSheet.getNumTileX(), Math.max(0, x));
		//y = Math.min(spriteSheet.getNumTileY(), Math.max(0, y));
	}

	int x = 121, y = 0;
	
	public void render(Graphics2D g) {
		g.setColor(Color.black);
		g.fillRect(0, 0, ctx.getScreenWidth(), ctx.getScreenHeight());

		g.setColor(Color.WHITE);
		
		int offsetX = 0;
		int offsetY = 0;
		int margin = 17;
		for (int y = 0; y < spriteSheet.getNumTileY(); y++) {
			for (int x = 0; x < spriteSheet.getNumTileX(); x++) {
				offsetX = x * spriteSheet.getTileWidth() + margin * x + 1;
				offsetY = y * spriteSheet.getTileHeight() + margin * y + 1;
				g.drawRect(offsetX, offsetY, spriteSheet.getTileWidth()+1, spriteSheet.getTileHeight()+1);
				spriteSheet.getSprite(x, y).draw(g, spriteSheet.getTileWidth()/2+offsetX+1, spriteSheet.getTileHeight()/2+offsetY+1);
				g.drawString(String.valueOf(x + y * spriteSheet.getNumTileX()), offsetX, offsetY);
			}
		}
		
				
	}

	public static void main(String[] args) {
		GameContainer gameContainer = new GameContainer(new SpriteSplit());
		gameContainer.setDisplayMode(1280, 800, 32);
		gameContainer.setFullScreen(false);
		gameContainer.start();
	}

}
